package com.zretc.controller;

import com.github.pagehelper.PageInfo;
import com.zretc.entity.Order;
import com.zretc.entity.Track;
import com.zretc.entity.Type;
import com.zretc.service.TrackService;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author马福又
 */
@Controller
@RequestMapping("/track")
public class TrackController {

    private static final Logger logger = LoggerFactory.getLogger(TrackService.class);

    @Autowired
    private TrackService trackService;


    @RequestMapping("/getOrderList")
    @Scope("prototype")
    @ResponseBody
    public  PageInfo<Order> getOrderList(@RequestParam(value = "pageNum") String pageNum,
                                         @RequestParam(value = "pageSize") String pageSize,
                                         @RequestParam(value = "opCode") String opCode){

        logger.debug("====pageNum"+pageNum);
        logger.debug("====pageSize"+pageSize);
        logger.debug("====opCode"+opCode);

        Map<String, String> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("opCode",opCode);

        PageInfo<Order> orderByPage = trackService.getOrderByPage(map);
        logger.debug("=====orderByPage"+orderByPage);
        return  orderByPage;
    }


    @RequestMapping( "/excel")
    public @ResponseBody
    boolean createExcel( @RequestParam(value = "opCode") String opCode,
                        @RequestParam(value = "orderId") String orderId){

        Map<String, String> map = new HashMap<>();
        map.put("opCode",opCode);
        map.put("orderId",orderId);
        List<Order> orders = trackService.getByExcel(map);
        String titleName = "仓库报表";
        String[] columnName = {"订单编号", "订单状态", "类型" ,"重量"};
        String fileName = "仓库报表";
        Integer[] columnWidth = { 30, 10, 10 ,10};
        String ex = trackService.getExcel(titleName,columnWidth,fileName,columnName,orders);
        if ("".equals(ex)||ex==null){
            return false;
        }
        return true;
    }

    @RequestMapping("/update")
    @ResponseBody
    public  int update(@RequestParam(value = "opCode") String opCode,
                       @RequestParam(value = "orderId") String orderId){

        logger.debug("====opCode"+opCode);
        logger.debug("====orderId"+orderId);

        Map<String, String> map = new HashMap<>();
        map.put("opCode",opCode);
        map.put("orderId",orderId);

         int updateOpCode = trackService.update(map);
        logger.debug("=====updateOpCode"+updateOpCode);
        return  updateOpCode;
    }


    @RequestMapping("/getOrderCondition")
    @Scope("prototype")
    @ResponseBody
    public  PageInfo<Order> getOrderCondition(@RequestParam(value = "pageNum") String pageNum,
                                         @RequestParam(value = "pageSize") String pageSize,
                                         @RequestParam(value = "orderId") String orderId,
                                         @RequestParam(value = "typeName") String typeName){

        logger.debug("====pageNum"+pageNum);
        logger.debug("====pageSize"+pageSize);
        logger.debug("====orderId"+orderId);
        logger.debug("====typeName"+typeName);

        Map<String, String> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("orderId",orderId);
        map.put("typeName",typeName);

        PageInfo<Order> orderByPage = trackService.getOrderByPage(map);
        logger.debug("=====orderByPage"+orderByPage);
        return  orderByPage;
    }

}
